package com.example.mq.rocketmq.consumer;

import com.example.mq.rocketmq.dataobject.Mq01Message;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 *
 * 消费失败处理逻辑
 *
 * 1.广播模式下不会进行重试消费
 * 2.集群模式，某个消费组失败了，只会对这个消费组进行重试。其他消费组不会进行重试
 *
 * @author luowj12
 * @since 2021/4/25 16:21
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = "DEMO_01",
        //消费模式，ORDERLY为顺序
        consumeMode = ConsumeMode.ORDERLY,
        //messageModel = MessageModel.BROADCASTING, // 设置为广播消费
        //消费者线程数
        consumeThreadMax =6,
        //selectorExpression = "tag_sync_store",
        consumerGroup = "consumer_01"
)
public class ConsumerListener implements RocketMQListener<Mq01Message> {
    @Override
    public void onMessage(Mq01Message message) {


        log.info("[onMessage][线程编号:{} 消息内容：{} 消息内容id:{}]", Thread.currentThread().getId(), message,message.getId());

        // sleep 2 秒，用于查看顺序消费的效果
        try {
            Thread.sleep(2 * 1000L);
        } catch (InterruptedException ignore) {
        }
        //测试消息重试
        //throw new RuntimeException();
    }
}
